﻿using BwTMusicModels;
using BwTServices.Music;
using BwTServices.Order;
using HelperModels;
using HelperModels.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BwTAdmin.Controllers
{
    public class OrderController : Controller
    {
        OrderServices _services = new OrderServices();
        MusicServices _music = new MusicServices();

        // GET: Order
        public ActionResult OrderManager()
        {
            return View();
        }
        public JsonResult GetAllOrders(OrderFilter P)
        {
            return Json(new { data = _services.GetAllOrders(P, out int total), statusCode = 1, message = "查询成功", success = true, totalCount = total }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult StartDealOrder(North_Order order,string userId)
        {
            var bResult = _services.RecodeOrderInfo(order.OrderId, null);
            string msg = "处理完成!";
            if (bResult < 1)
            {
                msg = "信息不匹配";
            }
            return Json(new { statusCode = bResult, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult VaildOrderIsExist(string key)
        {
            var bResult = _services.SearchOrderIsExist(Guid.Parse(key));
            string msg = "验证成功,开始处理。";
            if (bResult != 1)
            {
                msg = "此订单不存在!";
            }
            return Json(new { statusCode = bResult, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult DealMusicList(string lists)
        {
            return Json(new {data=_music.GetOrderMusic(lists), statusCode = 1, message = "获取成功", success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult CopyMusicInfoFile(List<MusicHelper> musics,string FileName,string Time)
        {
            var result = new CopyFiles().CopyMusicToDic(musics, FileName, Time);
            string msg = $"生成成功，共{musics.Count}个,完成{result.SuccessCount}个";
            int code = 1;
            if (result.SuccessCount == 0)
            {
                msg = "生成失败";
                code = 0;
            }
            return Json(new {statusCode = code, message = msg, success = true,error=result.ErrorName }, JsonRequestBehavior.AllowGet);

        }
        public JsonResult FinishDealMusic(North_Order order, string userId)
        {
            var bResult = _services.FinishDealOrder(order.OrderId, null);
            string msg = "提交成功!";
            if (bResult < 1)
            {
                msg = "提交失败";
            }
            return Json(new { statusCode = bResult, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
    }
}